package com.zp.drug.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.drug.entity.DrugImportDetail;
import com.zp.drug.entity.bo.DrugImportDetailBo;
import com.zp.drug.entity.bo.DrugImportMasterBo;
import com.zp.drug.entity.vo.DrugImportDetailVo;
import com.zp.drug.mapper.DrugImportDetailMapper;
import com.zp.drug.service.IDrugImportDetailService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 药品入库明细Service业务层处理
 *
 * @author zhang peng
 * @ date 2023-07-05
 */
@RequiredArgsConstructor
@Service
public class DrugImportDetailServiceImpl implements IDrugImportDetailService {

    private final DrugImportDetailMapper baseMapper;

    /**
     * 查询药品入库明细
     */
    @Override
    public DrugImportDetailVo queryById (String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询药品入库明细列表(分页)
     */
    @Override
    public TableDataInfo<DrugImportDetailVo> queryPageList (DrugImportDetailBo bo, PageQuery pageQuery) {
        Page<DrugImportDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询药品入库明细列表(不分页)
     */
    @Override
    public List<DrugImportDetailVo> queryList (DrugImportDetailBo bo) {
        LambdaQueryWrapper<DrugImportDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DrugImportDetail> buildQueryWrapper (DrugImportDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DrugImportDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getMasterId()), DrugImportDetail::getMasterId, bo.getMasterId());
        lqw.eq(StringUtils.isNotBlank(bo.getStockId()), DrugImportDetail::getStockId, bo.getStockId());
        lqw.eq(StringUtils.isNotBlank(bo.getItemNo()), DrugImportDetail::getItemNo, bo.getItemNo());
        lqw.eq(StringUtils.isNotBlank(bo.getDrugCode()), DrugImportDetail::getDrugCode, bo.getDrugCode());
        lqw.eq(StringUtils.isNotBlank(bo.getDrugInfoId()), DrugImportDetail::getDrugInfoId, bo.getDrugInfoId());
        lqw.eq(bo.getPackageNum() != null, DrugImportDetail::getPackageNum, bo.getPackageNum());
        lqw.eq(StringUtils.isNotBlank(bo.getPackageSpec()), DrugImportDetail::getPackageSpec, bo.getPackageSpec());
        lqw.eq(StringUtils.isNotBlank(bo.getPackageUnits()), DrugImportDetail::getPackageUnits, bo.getPackageUnits());
        lqw.eq(StringUtils.isNotBlank(bo.getMinSpec()), DrugImportDetail::getMinSpec, bo.getMinSpec());
        lqw.eq(StringUtils.isNotBlank(bo.getMinUnits()), DrugImportDetail::getMinUnits, bo.getMinUnits());
        lqw.eq(StringUtils.isNotBlank(bo.getPackSize()), DrugImportDetail::getPackSize, bo.getPackSize());
        lqw.eq(bo.getTradePrice() != null, DrugImportDetail::getTradePrice, bo.getTradePrice());
        lqw.eq(bo.getRetailPrice() != null, DrugImportDetail::getRetailPrice, bo.getRetailPrice());
        lqw.eq(bo.getDiscount() != null, DrugImportDetail::getDiscount, bo.getDiscount());
        lqw.eq(StringUtils.isNotBlank(bo.getBatchNo()), DrugImportDetail::getBatchNo, bo.getBatchNo());
        lqw.eq(bo.getProduceDate() != null, DrugImportDetail::getProduceDate, bo.getProduceDate());
        lqw.eq(bo.getExpireDate() != null, DrugImportDetail::getExpireDate, bo.getExpireDate());
        lqw.eq(bo.getQuantity() != null, DrugImportDetail::getQuantity, bo.getQuantity());
        lqw.eq(StringUtils.isNotBlank(bo.getOrgId()), DrugImportDetail::getOrgId, bo.getOrgId());
        lqw.eq(StringUtils.isNotBlank(bo.getLocation()), DrugImportDetail::getLocation, bo.getLocation());
        lqw.eq(StringUtils.isNotBlank(bo.getInvoiceNo()), DrugImportDetail::getInvoiceNo, bo.getInvoiceNo());
        lqw.eq(bo.getInvoiceDate() != null, DrugImportDetail::getInvoiceDate, bo.getInvoiceDate());
        lqw.eq(bo.getAllStock() != null, DrugImportDetail::getAllStock, bo.getAllStock());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), DrugImportDetail::getRemarks, bo.getRemarks());
        lqw.eq(bo.getProportion() != null, DrugImportDetail::getProportion, bo.getProportion());
        return lqw;
    }

    /**
     * 新增药品入库明细
     */
    @Override
    public Boolean insertByBo (DrugImportDetailBo bo) {
        DrugImportDetail add = BeanUtil.toBean(bo, DrugImportDetail.class);
        validEntityBeforeSave(add);
        add.initEntity();
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改药品入库明细
     */
    @Override
    public Boolean updateByBo (DrugImportDetailBo bo) {
        DrugImportDetail update = BeanUtil.toBean(bo, DrugImportDetail.class);
        update.updateInfo();
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave (DrugImportDetail entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除药品入库明细
     */
    @Override
    public Boolean deleteWithValidByIds (Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Transactional
    public List<DrugImportDetail> batchDrugImportInfo (DrugImportMasterBo bo) {
        List<DrugImportDetail> drugImportDetails = new ArrayList<>();
        baseMapper.deleteBatchIds(bo.getDetailListSave().stream().map(DrugImportDetailBo::getId).collect(Collectors.toList()));
        bo.getDetailListSave().forEach(item -> {
            DrugImportDetail drugImportDetail = BeanUtil.toBean(item, DrugImportDetail.class);
            drugImportDetail.initEntity();
            drugImportDetails.add(drugImportDetail);
        });
//        if (StringUtils.isNotBlank(bo.getId()) && StringUtils.equals(bo.getIsStaging(), UserConstants.YES) ||
//                StringUtils.isNotBlank(bo.getId()) && StringUtils.equals(bo.getIsStaging(), UserConstants.NO)) {
            baseMapper.insertBatch(drugImportDetails);
//        } else {
//            baseMapper.updateBatchById(drugImportDetails);
//        }
        return drugImportDetails;
    }
}
